/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.academico.dao.impl;

/**
 *
 * @author caseware03
 * 
 */
import com.academico.dao.BaseGenericDAO;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;

public abstract class AbstractBaseGenericDAOImpl<T, K extends Serializable> implements BaseGenericDAO<T, K> {

    @PersistenceContext
    protected EntityManager em;
    protected EntityManagerFactory emf = null;
    protected Class<T> classType;

    public AbstractBaseGenericDAOImpl() {
        super();
        this.classType = (Class<T>)getParameterClass(getClass(), 0);
        emf =  Persistence.createEntityManagerFactory("AcademicoPU");
        em =  this.getEntityManager();
    }

     private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    @Override
    public T create(T entity) {
        this.em.persist(entity);
        return entity;
    }

    @Override
    public T update(T entity) {
        return this.em.merge(entity);
    }

    @Override
    public void delete(T entity) {
        this.em.remove(entity);
    }

    @Override
    public T findById(K key) {
        return this.em.find(classType, key);
    }

    /**
     * El método privado getParameterClass el cual se utiliza para obtener el tipo de objeto de la clase parametrizada K.
     *
     */
    private static Class<?> getParameterClass(Class<?> clazz, int index) {
        return (Class<?>)(((ParameterizedType)clazz.getGenericSuperclass()).getActualTypeArguments()[index]);
    }
}